package com.ruoyi.common.security.interceptor;

import cn.dev33.satoken.SaManager;
import cn.dev33.satoken.stp.StpLogic;
import cn.dev33.satoken.stp.StpUtil;
import com.ruoyi.common.core.exception.ServiceException;
import com.ruoyi.common.redis.service.RedisService;
import org.apache.commons.lang3.text.StrBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.function.ServerResponse;

import javax.servlet.http.HttpServletRequest;
import java.util.concurrent.TimeUnit;

/**
 * @author resthx
 * @date 17:16 2022/6/1
 */
@Component
public class RedisNotRepeatSubmitHeaderInterceptor extends NotRepeatSubmitHeaderInterceptor{
    @Autowired
    private RedisService redisService;

    @Override
    protected boolean isRepeatSubmit(HttpServletRequest request) {
        StpLogic stpLogic = SaManager.getStpLogic(StpUtil.TYPE);
        String token = stpLogic.getTokenValue();
        String requestURI = request.getRequestURI();
        String key = token + requestURI;
        Boolean aBoolean = redisService.redisTemplate.opsForValue().setIfAbsent(key, true, 1L, TimeUnit.SECONDS);
        return !aBoolean;
    }
}
